function SearchBar(options) {
    var bar = {
        init: function (options) {
            var container = $(".searchBar.extend");
            if (!container) {
                return;
            }
            this.options = options;
            this._filterCount = 6;
            this._initCount = 0; //日期默认初始化
            if (this.options.app.visible === false) {
                this._initCount++;
            }
            if (this.options.platform.visible === false) {
                this._initCount++;
            }
            if (this.options.resType.visible === false) {
                this._initCount++;
            }
            if (this.options.channel.visible === false) {
                this._initCount++;
            }
            if (this.options.dataType.visible === false) {
                this._initCount++;
            }
            if (this.options.version.visible === false) {
                this._initCount++;
            }

            this._initRow();
            this._initAppUI();
            this._initPlatFormUI();
            this._initResTypeUI();
            this._initChannelUI();
            this._initDataTypeUI();

            this._initSearchBtn();

            this._initDataRangeUI();
            this._initVersionUI();
            this._initControl();
            this._initData();
        },

        getSearchValues: function () {
            var param = {
                startTime: this._startTime.toDate().getTime(),
                endTime: this._endTime.toDate().getTime()
            };
            if (this.options.app.visible !== false) {
                param.app = this._getSelectValue("appList", $("#selector_app").val());
            }
            if (this.options.resType.visible !== false) {
                param.resType = this._getSelectValue("resList", $("#selector_resType").val());
            }
            if (this.options.platform.visible !== false) {
                param.platform = this._getSelectValue("platformList", $("#selector_platform").val());
            }
            if (this.options.channel.visible !== false) {
                param.channel = this._getSelectValue("channelList", $("#selector_channel").val());
            }
            if (this.options.dataType.visible !== false) {
                param.dataType = this._getSelectValue("dataTypeList", $("#selector_dataType").val());
            }
            if (this.options.version.visible !== false) {
                param.version = this._getSelectValue("versionList", $("#selector_version").val());
            }

            return param;
        },

        _getSelectValue: function (listId, value) {
            var ret = [];
            var scope = $("body").scope();
            var list = scope[listId];
            if (!list) {
                return ret;
            }

            value = $.isArray(value) ? value : [value];
            for (var i = 0; i < list.length; i++) {
                var v = list[i].value + "";
                if (value.indexOf(v) >= 0) {
                    ret.push(list[i]);
                }
            }
            return ret;
        },

        _initRow: function () {
            var html = "<div class=\"searchBar row1 margin-v5 clearfix\"></div><div class=\"searchBar row2 margin-v5 clearfix\"></div>";
            $(".searchBar").append(html);
        },

        _initAppUI: function () {
            if (this.options.app.visible === false) {
                return;
            }
            var scope = $("body").scope();

            var html = "<div class=\"pull-left margin-h5\" style=\"width:160px;\">" +
                "<select id=\"selector_app\" class=\"select\" " +
                "style=\"width:160px;display:none\" " + (this.options.app.multiple ? "multiple" : "") + ">" +
                "<option ng-repeat=\"app in appList\" value=\"{{app.value}}\">{{app.title}}</option>" +
                "</select></div>";

            var el = scope.compile(html)(scope);
            $(".searchBar.row1").append(el);
        },

        _initPlatFormUI: function () {
            if (this.options.platform.visible === false) {
                return;
            }

            var scope = $("body").scope();
            var html = "<div class=\"pull-left margin-h5\" style=\"width:160px;\">" +
                "<select id=\"selector_platform\" class=\"select\" " +
                "style=\"width:160px;display:none\" " + (this.options.platform.multiple ? "multiple" : "") + ">" +
                "<option ng-repeat=\"platform in platformList\" value=\"{{platform.value}}\">{{platform.title}}</option>" +
                "</select></div>";

            var el = scope.compile(html)(scope);
            $(".searchBar.row1").append(el);
        },

        _initResTypeUI: function () {
            if (this.options.resType.visible === false) {
                return;
            }

            var scope = $("body").scope();
            var html = "<div class=\"pull-left margin-h5\" style=\"width:160px;\">" +
                "<select id=\"selector_resType\" class=\"select\" " +
                "style=\"width:160px;display:none\" " + (this.options.resType.multiple ? "multiple" : "") + ">" +
                "<option ng-repeat=\"res in resList\" value=\"{{res.value}}\">{{res.title}}</option>" +
                "</select></div>";

            var el = scope.compile(html)(scope);
            $(".searchBar.row1").append(el);
        },

        _initChannelUI: function () {
            if (this.options.channel.visible === false) {
                return;
            }
            var scope = $("body").scope();
            var html = "<div class=\"pull-left margin-h5\" style=\"width:160px;\">" +
                "<select id=\"selector_channel\" class=\"select\" " +
                "style=\"width:160px;display:none\" " + (this.options.channel.multiple ? "multiple" : "") + ">" +
                "<option ng-repeat=\"channel in channelList\" value=\"{{channel.value}}\">{{channel.title}}</option>" +
                "</select></div>";

            var el = scope.compile(html)(scope);
            $(".searchBar.row1").append(el);
        },

        _initDataTypeUI: function () {
            if (this.options.dataType.visible === false) {
                return;
            }

            var scope = $("body").scope();
            var html = "<div class=\"pull-left margin-h5\" style=\"width:160px;\">" +
                "<select id=\"selector_dataType\" class=\"select\" " +
                "style=\"width:160px;display:none\" " + (this.options.dataType.multiple ? "multiple" : "") + ">" +
                "<option ng-repeat=\"dataType in dataTypeList\" value=\"{{dataType.value}}\">{{dataType.title}}</option>" +
                "</select></div>";

            var el = scope.compile(html)(scope);
            $(".searchBar.row1").append(el);
        },

        _initSearchBtn: function () {
            var scope = $("body").scope();
            var html = "<div class=\"pull-right\">" +
                "<button class=\"btn blue\" ng-click=\"onSearchClick()\">搜索</button>" +
                "</div>";
            var el = scope.compile(html)(scope);
            $(".searchBar.row1").append(el);

            scope.safeApply(function () {
                if (!$.isFunction(this.options.searchHandler)) {
                    return;
                }
                scope.onSearchClick = this._onSearchClick.bind(this);
            }.bind(this));
        },

        _onSearchClick: function (isInit) {
            if (isInit) {
                this._initCount++;
                if (this._initCount < this._filterCount) {
                    return;
                }
            }


            var param = {
                appId: $("#selector_app").val(),
                resType: $("#selector_resType").val(),
                platform: $("#selector_platform").val(),
                channelId: $("#selector_channel").val(),
                dataType: $("#selector_dataType").val(),
                version: $("#selector_version").val(),
                startTime: this._startTime.toDate().getTime(),
                endTime: this._endTime.toDate().getTime()
            };
            this.options.searchHandler(param);
        },

        _initDataRangeUI: function () {
            var scope = $("body").scope();

            var html = "<div class=\"pull-left margin-h5\">" +
                "<div class=\"time-range btn btn-default\" style=\"border:1px solid #e5e5e5;background:white;display:inline-block\">" +
                "<i class=\"fa fa-calendar margin-r5\"></i>" +
                "<span></span>" +
                "<i class=\"caret\"></i>" +
                "</div>" +
                "</div>";

            var el = scope.compile(html)(scope);
            $(".searchBar.row2").append(el);
        },

        _initVersionUI: function () {
            if (this.options.version.visible === false) {
                return;
            }
            var scope = $("body").scope();
            var html = "<div class=\"pull-left margin-h5\" style=\"width:160px;\">" +
                "<select id=\"selector_version\" class=\"select\" " +
                "style=\"width:120px;display:none\" " + (this.options.version.multiple ? "multiple" : "") + ">" +
                "<option ng-repeat=\"version in versionList\" value=\"{{version.value}}\">{{version.title}}</option>" +
                "</select></div>";

            var el = scope.compile(html)(scope);
            $(".searchBar.row2").append(el);
        },

        _initControl: function () {
            var thisObj = this;
            $(".select").multiselect({
                numberDisplayed: 1,
                buttonWidth: "160px",
                allSelectedText: "",
                enableFiltering: false,
                includeSelectAllOption: false,
                selectAllValue: 'select-all-value',
                includeFilterClearBtn: false,
                maxHeight: 300,
                onChange: function(option, checked) {
                    if(this.$select[0].id == "selector_app") {
                        thisObj._loadVersionData($("#selector_app").val());
                    }
                }
            });

            $('.time-range').daterangepicker({
                    opens: 'right',
                    startDate: moment().subtract('days', 29),
                    endDate: moment(),
                    minDate: '01/01/2012',
                    maxDate: '12/31/2019',
                    dateLimit: {
                        days: 30 * 12 * 3
                    },
                    showDropdowns: true,
                    showWeekNumbers: true,
                    timePicker: false,
                    timePickerIncrement: 1,
                    timePicker12Hour: true,
                    ranges: {
                        '今日': [moment(), moment()],
                        '昨天': [moment().subtract('days', 1), moment().subtract('days', 1)],
                        '近 7 天': [moment().subtract('days', 6), moment()],
                        '近 30 天': [moment().subtract('days', 29), moment()],
                        '当前月': [moment().startOf('month'), moment().endOf('month')],
                        '上个月': [moment().subtract('month', 1).startOf('month'), moment().subtract('month', 1).endOf('month')]
                    },
                    buttonClasses: ['btn'],
                    applyClass: 'green',
                    cancelClass: 'default',
                    format: 'MM/DD/YYYY',
                    separator: ' 至 ',
                    locale: {
                        applyLabel: '确定',
                        cancelLabel: '取消',
                        fromLabel: '开始',
                        toLabel: '结束',
                        customRangeLabel: '自定义',
                        daysOfWeek: ['日', '一', '二', '三', '四', '五', '六'],
                        monthNames: ['1月', '2月', '3月', '4月', '5月', '6月',
                            '7月', '8月', '9月', '10月', '11月', '12月'],
                        firstDay: 1
                    }
                },
                function (start, end) {
                    this._startTime = start;
                    this._endTime = end;
                    $('.time-range span').html(start.format('YYYY-MM-DD') + ' - ' + end.format('YYYY-MM-DD'));
                }.bind(this)
            );

            this._startTime = moment().subtract(29, 'days');
            this._endTime = moment();
            //Set the initial state of the picker label
            $('.time-range span').html(this._startTime.format('YYYY-MM-DD') + ' - ' + this._endTime.format('YYYY-MM-DD'));
        },

        _initData: function () {
            this._loadAppData();
            this._loadPlatformData();
            this._loadResTypeData();
            this._loadChannelData();
            this._loadDataTypeData();
        },

        _loadAppData: function () {
            if (this.options.app.visible !== false) {
                this._loadSelectData("selector_app", "appList", this.options.app.url, function (data) {
                    return data;
                }, function(value) {
                    this._loadVersionData(value);
                }.bind(this));
            }
        },

        _loadPlatformData: function () {
            if (this.options.platform.visible !== false) {
                this._loadSelectData("selector_platform", "platformList", this.options.platform.url, function (data) {

                    var first = this.options.platform.first;
                    if (first && first >= 1) {
                        for (var i = 0; i < first; i++) {
                            data.remove(0);
                        }
                    }

                    return data;
                }.bind(this));
            }
        },

        _loadResTypeData: function () {
            if (this.options.resType.visible !== false) {
                this._loadSelectData("selector_resType", "resList", this.options.resType.url, function (data) {
                    return data;
                });
            }
        },


        _loadChannelData: function () {
            if (this.options.channel.visible !== false) {
                this._loadSelectData("selector_channel", "channelList", this.options.channel.url, function (data) {
                    return data;
                });
            }
        },

        _loadDataTypeData: function () {
            if (this.options.dataType.visible !== false) {
                this._loadSelectData("selector_dataType", "dataTypeList", this.options.dataType.url, function (data) {
                    return data;
                });
            }
        },

        _loadVersionData: function (appId) {
            if (this.options.version.visible !== false) {
                this._loadSelectData("selector_version", "versionList", this.options.version.url+"?appId="+appId, function (data) {
                    var first = this.options.version.first;
                    if (first && first >= 1) {
                        for (var i = 0; i < first; i++) {
                            data.remove(0);
                        }
                    }
                    return data;
                }.bind(this));
            }
        },

        _loadSelectData: function (id, scopeKey, url, parse, callback) {
            var url = createUrl(url);
            $.get(url, function (data) {
                var jsData = data;
                if (jsData.code == 0) {
                    var list = parse(jsData.data);
                    if ($.isArray(list) && list.length > 0) {
                        var scope = $('body').scope();
                        scope.safeApply(function () {
                            scope[scopeKey] = list;
                        });

                        window.setTimeout(function () {
                            var sid = "#" + id;
                            $(sid).multiselect("rebuild");
                            var select = list[0].value;
                            $(sid).multiselect('select', select);
                            $(sid).multiselect('refresh');

                            if (callback && $.isFunction(callback)) {
                                callback(select);
                            }
                            this._onSearchClick(true);
                        }.bind(this), 0);
                    } else {
                        var scope = $('body').scope();
                        scope.safeApply(function () {
                            scope[scopeKey] = [];
                        });

                        window.setTimeout(function () {
                            var sid = "#" + id;
                            $(sid).multiselect("rebuild");
                            $(sid).multiselect('refresh');
                        }.bind(this), 0);
                    }
                }
            }.bind(this)).error(function () {
                alert("数据加载失败！");
            });
        }
    };
    bar.init(options);
    return bar;
};